import Vue from 'vue';
import App from './App.vue';
import ElementUI from 'element-ui';
import filters from './utils/filters';
import 'element-ui/lib/theme-chalk/index.css';
import router from './router';
import api from './api/index.js';
import bus from './utils/bus';
import lodash from 'lodash';
import { getRouterInfoByType } from './router/routerInfo';
import modal from './pages/common/modal';
import  './assets/css/common.scss';
import  './assets/css/styleMix.scss';
import styleVariables from './assets/css/styleInJs.scss';
import './assets/icons/iconfont.css';
import './assets/icons/iconfont.js';
import {htmlRoutersConfig} from './router/routerConfig';
import footerButtons from '@/pages/common/footerButtons';
Vue.use(ElementUI, { size: 'small' });
Vue.component('modal', modal);
Vue.component('footerButtons', footerButtons);
Vue.config.productionTip = false;
// 将API方法绑定到全局
Vue.prototype.$api = api;
Vue.prototype.$bus = bus;
Vue.prototype.$styleVariables = styleVariables;

const _ = lodash;

//路由拦截
router.beforeEach((to, from, next) => {

    //主页菜单切换无需登录和token
    let find = htmlRoutersConfig.find(item =>{
        return item.path === to.path
    });
    if(find){
        next();
        return
    }
    let token = sessionStorage.getItem('token');
    if (token && to.path !== '/') {
        if (to.matched.length) {
            let fromInfo = getRouterInfoByType({ path: from.path });
            let toInfo = getRouterInfoByType({ path: to.path });
            if (toInfo) {
                bus.$emit('menuChange', toInfo, to.query);
            } else {
                bus.$emit('menuChange', null, to.query);
            }
            next();
        } else {
            next({ name: 'sceneList' });
        }
    } else if (to.path === '/') {
        sessionStorage.clear();
        next();
    } else {
        next({ path: '/' });
    }
});

new Vue({
    el: '#app',
    router,
    render: h => h(App),
});